package artid.com.arttogoa.adapter;

import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import artid.com.arttogoa.MyDeliverGoods_Activity;
import artid.com.arttogoa.R;
import artid.com.arttogoa.parser.TransactionManagementParser;
import artid.com.arttogoa.util.NetUtils;
import artid.com.arttogoa.util.SendHxMessage_Utils;
import com.nostra13.universalimageloader.core.ImageLoader;

/**
 * 
*    
* 项目名称：ArtworkArtA   
* 类名称   ：TransactionManagement_Adapter   
* 类描述   ：交易管理适配器   
* 创建人   ：耿卫斌   
* 创建时间：2016-8-23 上午10:18:54   
*
 */
public class TransactionManagement_Adapter extends BaseAdapter{
	private Context mContext;
	private List<TransactionManagementParser> list;
	private int  whichActivity=0;//从哪个页面的适配器(0：全部  1：待发货 2：买家待付款 3：已发货 4：已出售 5：已关闭)                                                               

	public TransactionManagement_Adapter(Context mContext,
			List<TransactionManagementParser> list, int whichActivity) {
		super();
		this.mContext = mContext;
		this.list = list;
		this.whichActivity = whichActivity;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		View v = convertView;
		if (v == null) {
			holder = new ViewHolder();
			v = LayoutInflater.from(mContext).inflate(R.layout.item_transactionmanagement, null);
			holder.contactBuyerRL=(RelativeLayout) v.findViewById(R.id.item_transactionmanagement_contactBuyerRL);
			holder.img=(ImageView) v.findViewById(R.id.item_transactionmanagement_img);
			holder.orderType=(TextView) v.findViewById(R.id.item_transactionmanagement_orderType);
			holder.title=(TextView) v.findViewById(R.id.item_transactionmanagement_title);
			holder.isguarantee=(TextView) v.findViewById(R.id.item_transactionmanagement_isguarantee);
			holder.price=(TextView) v.findViewById(R.id.item_transactionmanagement_price);
			holder.deliverGoodsBtn=(TextView) v.findViewById(R.id.item_transactionmanagement_deliverGoodsBtn);
			v.setTag(holder);
		} else {
			holder = (ViewHolder) v.getTag();
		}
		ImageLoader.getInstance().displayImage(list.get(position).getGoods_image()
				+ "_"+ NetUtils.dip2px(mContext, 100)+"x"+NetUtils.dip2px(mContext, 100)+".png"
				, holder.img);
		holder.title.setText(list.get(position).getRealname()+"《"+ list.get(position).getGoods_name()+"》");
		holder.price.setText("￥"+list.get(position).getGoods_price());
		if("1".equals(list.get(position).getIs_auth())){
			//已备案
			holder.isguarantee.setVisibility(View.VISIBLE);
		}else{
			holder.isguarantee.setVisibility(View.GONE);
		}
		//初始化隐藏我要发货按钮防止全部里显示出错
		holder.deliverGoodsBtn.setVisibility(View.GONE);
		//初始化订单状态颜色防止全部里显示出错
		holder.orderType.setTextColor(Color.parseColor("#DDA42C"));
		String order_state=list.get(position).getOrder_state();
		if("5".equals(order_state)){
			//全部

		}else if("20".equals(order_state)){
			//待发货
			holder.orderType.setText("等待卖家发货");
			holder.deliverGoodsBtn.setVisibility(View.VISIBLE);
		}else if("10".equals(order_state)){
			//买家待付款
			holder.orderType.setText("买家待付款");
		}else if("30".equals(order_state)){
			//已发货
			holder.orderType.setText("已发货");
		}else if("40".equals(order_state)){
			//已出售
			holder.orderType.setText("已出售");
		}else if("0".equals(order_state)){
			holder.orderType.setText("已关闭");
			holder.orderType.setTextColor(Color.parseColor("#AFAFAF"));
		}
		holder.contactBuyerRL.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				// 进入聊天页面
				SendHxMessage_Utils.sendMessage(mContext,list.get(position).getBuyer_id(), list.get(position).getBuyer_nickname(),
						list.get(position).getBuyer_avatar(), list.get(position).getGoods_name(), list.get(position).getGoods_image());
			}
		});
		holder.deliverGoodsBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent= new Intent(mContext,MyDeliverGoods_Activity.class);
				intent.putExtra("order_id", list.get(position).getOrder_id());
				mContext.startActivity(intent);
			}
		});
		return v;
	}
	class ViewHolder {
		RelativeLayout contactBuyerRL;
		TextView orderType,title,isguarantee,price,deliverGoodsBtn;
		ImageView img;
	}
}
